package com.gnerv.battle.boot.project.guard.business.impl;

import com.gnerv.battle.boot.common.base.model.RestResult;
import com.gnerv.battle.boot.common.tools.UUIDTools;
import com.gnerv.battle.boot.project.guard.business.MonitorMiddlewareBusiness;
import com.gnerv.battle.boot.project.guard.business.MonitorMiddlewareBusiness;
import com.gnerv.battle.boot.project.guard.model.bo.MonitorMiddlewareBO;
import com.gnerv.battle.boot.project.guard.model.dto.MonitorMiddlewareCreateDTO;
import com.gnerv.battle.boot.project.guard.model.dto.MonitorMiddlewareDTO;
import com.gnerv.battle.boot.project.guard.model.dto.MonitorMiddlewareUpdateDTO;
import com.gnerv.battle.boot.project.guard.service.MonitorMiddlewareService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class MonitorMiddlewareBusinessImpl implements MonitorMiddlewareBusiness {

    private final MonitorMiddlewareService monitorMiddlewareService;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> create(MonitorMiddlewareCreateDTO monitorMiddlewareCreateDTO) {
        MonitorMiddlewareBO monitorMiddlewareBO = monitorMiddlewareCreateDTO.toMonitorMiddlewareBO();
        monitorMiddlewareBO.setUkId(UUIDTools.lower());
        monitorMiddlewareService.create(monitorMiddlewareBO);
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> deleteByUkId(String ukId) {
        monitorMiddlewareService.deleteByUkId(ukId);
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> updateByUkId(MonitorMiddlewareUpdateDTO monitorMiddlewareUpdateDTO) {
        monitorMiddlewareService.updateByUkId(monitorMiddlewareUpdateDTO.toMonitorMiddlewareBO());
        return RestResult.ok();
    }

    @Override
    @Transactional(readOnly = true)
    public RestResult<MonitorMiddlewareDTO> selectByUkId(String ukId) {
        return RestResult.ok(monitorMiddlewareService.selectByUkId(ukId));
    }

    @Override
    @Transactional(readOnly = true)
    public RestResult<List<MonitorMiddlewareDTO>> listByName(String name, Integer page, Integer size) {
        return RestResult.ok(monitorMiddlewareService.listByName(name, page, size));
    }

}
